//引入vue
import Vue from 'vue';
import axios from 'axios';
import qs from 'qs'

//全局状态控制引入
import store from '../store/index';
import '../plugins/element'
import * as types from '../store/mutation-types';
import router from '../router'

// http request 拦截器
axios.interceptors.request.use(
    config => {
        if (store.state.userInfo.token) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers['Authorization'] = `Bearer ${store.state.userInfo.token}`;
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    });

/*let _this = this;*/
// http response 拦截器
axios.interceptors.response.use(
    undefined,
    error => {
        let res = error.response;

        switch (res.status) {
            case 401:
                // 返回 401 清除token信息并跳转到登录页面
                /*store.commit(types.LOGOUT);
                router.replace({
                    path: '/app/login',
                    query: {redirect: router.currentRoute.fullPath}
                });*/
                router.replace({
                    path: '/app/login',
                    query: {redirect: router.currentRoute.fullPath}
                });
                break;
        }
        return Promise.reject(error.response.data)   // 返回接口返回的错误信息
    });

